﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Common.DTO;
using Common.DTO.ihopplockDTO;
using LogicLayer;

namespace ImmoralServices
{
    /// <summary>
    /// Facade for calls regarding the game
    /// </summary>
    public class GameService : IGameService
    {
        #region GameLobby

        // --- INFO ---

        public List<GameType> GetListOfAllGameTypes()
        {
            return GameLobbyHandler.GetGameTypeList();
        }

        /// <summary>
        /// Get a list of Games which are active and
        /// the Player is one if the participants
        /// </summary>
        /// <param name="playerAskingId">The PlayerID asking for the list</param>
        /// <param name="gameTypeId">The type of game playing</param>
        /// <returns></returns>
        public List<PlayerActiveGame_Init> GetListOfPlayersActiveGamesByGameType_Init(int playerAskingId, int gameTypeId)
        {
            return GameLobbyHandler.GetPlayersActiveGameList_Init(playerAskingId, gameTypeId);
        }

        public List<PlayerActiveGame_Init> GetListOfPlayersActiveGamesByGameType_Poll(int playerAskingId, int gameTypeId)
        {
            throw new NotImplementedException();
        }

        public List<JoinableGame> GetListOfJoinableGames(int gameTypeID, int playerAskingId)
        {
            return GameLobbyHandler.GetAllJoinableGames(gameTypeID, playerAskingId);
        }

        public List<GameNotStarted> GetListOfGamesNotStarted(int playerAskingId)
        {
            return GameLobbyHandler.GetAllGamesNotStartedWithPlayer(playerAskingId);
        }

        // --- ACTIONS ---

        /// <summary>
        /// Creates a Game and 
        /// creates a post in PlayerGame with the creating Player
        /// </summary>
        /// <param name="playerId">The player who creates the Game</param>
        /// <param name="gameTypeId">The type of game to play</param>
        /// <param name="rounds">Number of gamerounds</param>
        /// <returns>An object containing info to be displayed in a game lobby</returns>
        public GameLobby CreateGame(int playerId, int gameTypeId, int rounds)
        {
            return GameLobbyHandler.CreateGame(playerId, gameTypeId, rounds);
        }

        // TODO_OL: Säkerhet. Ta in token/spelareID för att se om spelaren får göra det.
        public bool DeleteGame(int gameId)
        {
            return GameLobbyHandler.DeleteGame(gameId);
        }

        // TODO_OL: Säkerhet. Ta in token/spelareID för att se om spelaren får göra det.
        public bool UpdateGameSettings(int gameId, int gameTypeId, int rounds)
        {
            return GameLobbyHandler.UpdateGame(gameId, gameTypeId, rounds);
        }

        public bool JoinGame(int gameId, int playerId)
        {
            return GameLobbyHandler.JoinGame(gameId, playerId);
        }

        public bool JoinRandomGame(int playerId, int gameTypeId)
        {
            return GameLobbyHandler.JoinRandomGame(playerId, gameTypeId);
        }

        public bool LeaveGame(int gameId, int playerId)
        {
            return GameLobbyHandler.RemovePlayerFromGame(gameId, playerId);
        }

        #endregion

        #region InGame

        // --- INFO ---

        /// <summary>
        /// Checks if opponent of the asking player in the provided game has made a choice
        /// </summary>
        /// <param name="gameId">ID of game playing</param>
        /// <param name="playerAskingId">ID of the player asking</param>
        /// <returns>True if opponent has made a choice</returns>
        public bool HasOpponentMadeChoice(int gameId, int playerAskingId)
        {
            return InGameHandler.HasOpponentMadeChoice(gameId, playerAskingId);
        }

        public ActiveGameInfo_Init GetActiveGameInfo_Init(int gameId, int playerAskingId)
        {
            return InGameHandler.GetActiveGameInfo_Init(gameId, playerAskingId);
        }

        public ActiveGameInfo_Poll GetActiveGameInfo_Poll(int gameId, int playerAskingId)
        {
            return InGameHandler.GetActiveGameInfo_Poll(gameId, playerAskingId);
        }


        // --- ACTIONS ---

        /// <summary>
        /// One of the Players in a game makes a choice
        /// </summary>
        /// <param name="gameId">ID of the Game</param>
        /// <param name="playerId">ID of the Player makeing the choice</param>
        /// <param name="playerChoice">The choice made</param>
        /// <returns></returns>
        public bool MakePlayerChoice(int gameId, int playerId, int playerChoice)
        {
            return InGameHandler.MakePlayerChoice(gameId, playerId, playerChoice);
        }

        #endregion

        // Metoder för admins. Intressant data
        #region ADMIN

        // TODO_OL: Gör en säkerhetskoll att anropande användare är en admin
        public List<Game> x_GetAllActiveGames()
        {
            return GameLobbyHandler.GetAllActiveGames();
        }

        #endregion

#if DEBUG

        public void TestOskar()
        {
            InGameHandler.TestOskar();
        }

#endif

    }
}
